//
//  DYSelectColorView.m
//  colorTest
//
//  Created by 丁益 on 2017/4/9.
//  Copyright © 2017年 celi. All rights reserved.
//

#import "DYSelectColorView.h"
#import "UIView+Extension.h"
#import "DYColorView.h"
#import "DYSingleColorView.h"


@interface DYSelectColorView()

@property (nonatomic,copy) NSString *redValue;
@property (nonatomic,copy) NSString *greenValue;
@property (nonatomic,copy) NSString *blueValue;
@property (nonatomic,weak) DYSingleColorView *redColorView;
@property (nonatomic,weak) DYSingleColorView *blueColorView;
@property (nonatomic,weak) DYSingleColorView *greenColorView;
@property (nonatomic,weak) UIButton *btnCover;
@end

@implementation DYSelectColorView

- (id) initWithSelectColorWithRedValue:(NSString *)redValue blueValue:(NSString *)blueValue greenValue:(NSString *)greenValue
{
    
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if(self){
        self.redValue = redValue;
        self.greenValue = greenValue;
        self.blueValue = blueValue;
        UIButton *btnCover = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, sWidth, sHeight)];
        [btnCover setBackgroundColor:[UIColor colorWithRed:88/255.0 green:88/255.0 blue:88/255.0 alpha:0.6]];
        [btnCover addTarget:self action:@selector(btnCoverOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnCover];
        
        CGFloat selectColorViewW = sWidth*0.8;
        CGFloat selectColorViewH = 200;//(sHeight-64)*0.6;
        UIView *selectColorView = [[UIView alloc]initWithFrame:CGRectMake(0, 84, selectColorViewW, selectColorViewH)];
        selectColorView.backgroundColor = [UIColor whiteColor];
        selectColorView.layer.borderWidth = 1.0;
        selectColorView.layer.cornerRadius = 5;
        selectColorView.layer.masksToBounds = YES;
        selectColorView.layer.borderColor = [UIColor grayColor].CGColor;
        selectColorView.centerX = sWidth/2;
        
        
        UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, sWidth, 25)];
        titelLabel.text = @"选择颜色";
        titelLabel.centerX = selectColorView.width/2;
        titelLabel.textAlignment = NSTextAlignmentCenter;
        titelLabel.textColor = [UIColor blackColor];
        [selectColorView addSubview:titelLabel];
        
        CGFloat redColorViewY = CGRectGetMaxY(titelLabel.frame)+20;
        DYSingleColorView *redColorView = [[DYSingleColorView alloc]initWithFrame:CGRectMake(10, redColorViewY, 45, 20) selectColorWithValue:redValue color:[UIColor redColor]];
        [selectColorView addSubview:redColorView];
        CGFloat redSliderY = CGRectGetMaxY(redColorView.frame)+10;
        UISlider *redSlider = [[UISlider alloc]initWithFrame:CGRectMake(10, redSliderY, selectColorView.width-40, 20)];
        redSlider.value = [redValue integerValue]/255.0;
        [redSlider addTarget:self action:@selector(redSliderChanged:) forControlEvents:UIControlEventValueChanged];
        [selectColorView addSubview:redSlider];
        self.redColorView = redColorView;
        
        
        CGFloat greenColorViewY = CGRectGetMaxY(redSlider.frame)+25;
        DYSingleColorView *greenView = [[DYSingleColorView alloc]initWithFrame:CGRectMake(10, greenColorViewY, 45, 20) selectColorWithValue:greenValue color:[UIColor greenColor]];
        [selectColorView addSubview:greenView];
        CGFloat greenSliderY = CGRectGetMaxY(greenView.frame)+10;
        UISlider *greenSlider = [[UISlider alloc]initWithFrame:CGRectMake(10, greenSliderY, selectColorView.width-40, 20)];
        greenSlider.value = [greenValue integerValue]/255.0;
        [greenSlider addTarget:self action:@selector(greenSliderChanged:) forControlEvents:UIControlEventValueChanged];
        [selectColorView addSubview:greenSlider];
        self.greenColorView = greenView;
        
        CGFloat blueColorViewY = CGRectGetMaxY(greenSlider.frame)+25;
        DYSingleColorView *blueView = [[DYSingleColorView alloc]initWithFrame:CGRectMake(10, blueColorViewY, 45, 20) selectColorWithValue:blueValue color:[UIColor blueColor]];
        [selectColorView addSubview:blueView];
        CGFloat blueSliderY = CGRectGetMaxY(blueView.frame)+10;
        UISlider *blueSlider = [[UISlider alloc]initWithFrame:CGRectMake(10, blueSliderY, selectColorView.width-40, 20)];
        blueSlider.value = [blueValue integerValue]/255.0;
        [blueSlider addTarget:self action:@selector(blueSliderChanged:) forControlEvents:UIControlEventValueChanged];
        [selectColorView addSubview:blueSlider];
        self.blueColorView = blueView;
        
        CGFloat btnOkY = CGRectGetMaxY(blueSlider.frame) + 10;
        CGFloat btnOkW = 80;
        UIButton *btnOk = [[UIButton alloc]initWithFrame:CGRectMake(0, btnOkY, btnOkW, 30)];
        btnOk.centerX = selectColorView.width/4;
        [btnOk setTitle:@"确定" forState:UIControlStateNormal];
        [btnOk setTitleColor:DYColor(135, 205, 145) forState:UIControlStateNormal];
        [btnOk setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
        [btnOk addTarget:self action:@selector(btnOkOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [selectColorView addSubview:btnOk];
        
        
        UIButton *btnCancel = [[UIButton alloc]initWithFrame:CGRectMake(0, btnOkY, btnOkW, 30)];
        btnCancel.centerX = selectColorView.width*0.75;
        [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
        [btnCancel setTitleColor:DYColor(135, 205, 145) forState:UIControlStateNormal];
        [btnCancel setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
        [btnCancel addTarget:self action:@selector(btnCancelOnclick:) forControlEvents:UIControlEventTouchUpInside];
        [selectColorView addSubview:btnCancel];
        
        selectColorView.height = CGRectGetMaxY(btnCancel.frame)+10;
        self.btnCover = btnCover;
        self.hidden = YES;
        [self addSubview:selectColorView];
    }
    return self;
}

- (void)showColorView {
    UIView *view = [[UIApplication sharedApplication].windows lastObject];
    [view addSubview:self];
    self.hidden = NO;
}

- (void)hiddenColorView {
    
    [self endEditing:YES];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


- (void)btnCoverOnclick:(UIButton*)btn{
    //退出试图
    DLog(@"btnCoverOnclick--");
    [self hiddenColorView];
}


#pragma mark - Slider changed
- (void)redSliderChanged:(UISlider *)slider
{
    DLog(@"redSliderChanged");
    self.redValue = [NSString stringWithFormat:@"%0.0f",slider.value*255];
    [self.redColorView setColorValue:self.redValue];
    
}

- (void)greenSliderChanged:(UISlider *)slider
{
    DLog(@"greenSliderChanged");
    self.greenValue = [NSString stringWithFormat:@"%0.0f",slider.value*255];
    [self.greenColorView setColorValue:self.greenValue];
}

- (void)blueSliderChanged:(UISlider *)slider
{
    DLog(@"blueSliderChanged");
    self.blueValue = [NSString stringWithFormat:@"%0.0f",slider.value*255];
    [self.blueColorView setColorValue:self.blueValue];
}

- (void)btnOkOnclick:(UIButton *)btn
{
    DLog(@"btnOkOnclick");
    if ([self.delegate respondsToSelector:@selector(selectColorWithRedValue:blueValue:greenValue:)]) {
        [self.delegate selectColorWithRedValue:self.redValue blueValue:self.blueValue greenValue:self.greenValue];
    }
    //退出试图
    [self hiddenColorView];
}

- (void)btnCancelOnclick:(UIButton *)btn
{
    DLog(@"btnCancelOnclick");
    //退出试图
    [self hiddenColorView];
}

@end
